package com.sgb.merchant.viewmodel;

import android.app.Application;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.link.api.CommonCallback;
import com.sgb.link.api.CommonNet;
import com.sgb.link.entity.AMapAddressEntity;
import com.sgb.link.entity.merchant.EditIndustryInfoEntity;
import com.sgb.link.entity.merchant.IndustryCategoryEntity;
import com.sgb.link.entity.merchant.PublishIndustryEntity;
import com.sgb.merchant.api.MerchantsNet;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseViewModel;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * @author: 王彬臣
 * @date: 2021/10/15
 * @Description:
 */
public class PublishIndustryViewModel extends BaseViewModel {

    public MutableLiveData<List<IndustryCategoryEntity>> ldLeve = new MutableLiveData<>();
    public MutableLiveData<List<IndustryCategoryEntity>> ldGroup = new MutableLiveData<>();
    public MutableLiveData<List<IndustryCategoryEntity>> ldType = new MutableLiveData<>();
    public MutableLiveData<List<IndustryCategoryEntity>> ldDevice = new MutableLiveData<>();
    public MutableLiveData<List<IndustryCategoryEntity>> ldDiscount = new MutableLiveData<>();
    public MutableLiveData<List<String>> uploadUrls = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldResult = new MutableLiveData<>();
    public MutableLiveData<String> ldUrl = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldState = new MutableLiveData<>();
    public MutableLiveData<EditIndustryInfoEntity> ldData = new MutableLiveData<>();


    public PublishIndustryViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取地址
     */
    public AMapAddressEntity get(Intent data) {
        if (data == null) {
            return null;
        }
        return data.getParcelableExtra(RouterKey.DATA);
    }

    public void getType() {
        MerchantsNet.getInstance().getService().getParkTypes().enqueue(new ICallBack<BaseEntity<List<IndustryCategoryEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<IndustryCategoryEntity>> data) {
                if (data.isSuccess()) {
                    ldType.postValue(data.getData());
                }
            }
        });
    }

    public void getLevel() {
        MerchantsNet.getInstance().getService().getParkLevels().enqueue(new ICallBack<BaseEntity<List<IndustryCategoryEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<IndustryCategoryEntity>> data) {
                if (data.isSuccess()) {
                    ldLeve.postValue(data.getData());
                }
            }
        });
    }

    public void getGroup() {
        MerchantsNet.getInstance().getService().getParkClusters().enqueue(new ICallBack<BaseEntity<List<IndustryCategoryEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<IndustryCategoryEntity>> data) {
                if (data.isSuccess()) {
                    ldGroup.postValue(data.getData());
                }
            }
        });
    }

    public void getDiscount() {
        MerchantsNet.getInstance().getService().getParkDiscount().enqueue(new ICallBack<BaseEntity<List<IndustryCategoryEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<IndustryCategoryEntity>> data) {
                if (data.isSuccess()) {
                    ldDiscount.postValue(data.getData());
                }
            }
        });
    }

    public void getDevice() {
        MerchantsNet.getInstance().getService().getParkDevice().enqueue(new ICallBack<BaseEntity<List<IndustryCategoryEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<IndustryCategoryEntity>> data) {
                if (data.isSuccess()) {
                    ldDevice.postValue(data.getData());
                }
            }
        });
    }

    public void uploadMutiFileModel(List<File> files) {
        Map<String, RequestBody> photos = new HashMap<>();
        int i = 0;
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            photos.put("file" + i + "\"; filename=\"" + i + "image.jpg\"", requestBody);
            i++;
        }
        CommonNet.getInstance().getService().uploadMutiFileDrive(photos).enqueue(new ICallBack<BaseEntity<List<String>>>() {
            @Override
            public void onSuccess(BaseEntity<List<String>> data) {
                if (data.isSuccess()) {
                    uploadUrls.postValue(data.getData());
                }

            }
        });
    }

    public void editPark(PublishIndustryEntity publishIndustryEntity) {
        MerchantsNet.getInstance().getService().editParks(publishIndustryEntity).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.isSuccess()) {
                    ldResult.postValue(true);
                }
            }
        });
    }

    public void uploadFile(File file) {

        try {
            Map<String, RequestBody> photos = new HashMap<>();
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            String fileName = URLEncoder.encode(file.getName(), "UTF-8");
            photos.put("file" + "\"; filename=\"" + fileName, requestBody);
            MerchantsNet.getInstance().getService().addFile(photos).enqueue(new ICallBack<BaseEntity>() {
                @Override
                public void onSuccess(BaseEntity data) {
                    if (data.isSuccess()) {
                        ldUrl.postValue(data.getData().toString());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity> call, Throwable t) {
                    super.onFailure(call, t);
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getCurrentState(String no) {
        MerchantsNet.getInstance().getService().getIndustryState(no).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if (data.isSuccess()) {//0已下架 1已删除 2已上架
                    if (data.getData() != 0) {//若已上架或已删除，则弹出弹窗，关闭弹窗后跳转至管理页面
                        ldState.postValue(false);
                    } else {
                        ldState.postValue(true);
                    }
                }
            }
        });
    }

    public void getParkInfo(String no) {
        MerchantsNet.getInstance().getService().getParksDetailInfo(no).enqueue(new ICallBack<BaseEntity<EditIndustryInfoEntity>>() {
            @Override
            public void onSuccess(BaseEntity<EditIndustryInfoEntity> data) {
                ldData.postValue(data.getData());
            }
        });
    }
}
